# 抖码算法，让算法学习变得简单有趣
# 作者：老汤

s = str(input()).split(" ")

# 剪贴板
temp = ""
# 屏幕
screen = ""
# 选择的
select = ""

for i in range(len(s)):
    if s[i] == "1": # a 键在屏幕上输出一个字母 a
        if select != "":
            screen, select = "a", ""
        else:
            screen += "a"
    elif s[i] == "2": # ctrl-c 将当前选择的字母复制到剪切板
        if select != "":
            temp = select
    elif s[i] == "3": # ctrl-x 将当前选择的字母复制到剪贴板，并清空选择的字母
        if select != "":
            temp, screen, select = select, "", ""
    elif s[i] == "4": # ctrl-v 将当前剪切板里的字母输出到屏幕
        if select != "":
            screen, select = temp, ""
        else:
            screen += temp
    elif s[i] == "5": # ctrl-a 选择当前屏幕上的所有字母
        if screen != "":
            select = screen

print(len(screen))